package com.fun_ordering.service.impl;


import com.fun_ordering.entity.LoginUser;
import com.fun_ordering.mapper.FoEmployeeMapper;
import com.fun_ordering.pojo.FoEmployee;
import com.fun_ordering.pojo.FoEmployeeExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 陈星
 * @date 2024/3/24 14:30
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private FoEmployeeMapper foEmployeeMapper;
    /**
     *
     * 作用：
     * 方法名：登录名
     * 参数：username
     * 返回值：
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1，通过账号查询当前信息
        FoEmployeeExample example = new FoEmployeeExample();
        FoEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpAccountEqualTo(username);
        List<FoEmployee> foEmployees = foEmployeeMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(foEmployees)){
            return null;
        }
        FoEmployee foEmployee = foEmployees.get(0);
        //entity中的工具类new了一个有参的构造器，这样就可以拿到值进行校验了
        return new LoginUser(foEmployee);
    }
}
